const { app, BrowserWindow, ipcMain } = require('electron');
const path = require('path');


let mainWindow;
function createMainWindow() {
    mainWindow = new BrowserWindow({
        x: 100,
        y: 100,
        show: false,
        width: 800,
        height: 600,
        alwaysOnTop: true,
        autoHideMenuBar: true,
        title: '主界面',
        webPreferences: {
            preload: path.join(__dirname, '/script/preload.js')
        }
    });

    mainWindow.once('ready-to-show', () => {
        mainWindow.show();
    });
    mainWindow.on('closed', () => {
        mainWindow = null;
    });
    mainWindow.loadFile(path.join(__dirname, '/index.html'));

    // mainWindow.webContents.openDevTools();

    
}

//*************进程间通信**********/
ipcMain.handle('inject-script', () => {
    return path.join(app.getAppPath(), '/node_modules/handsontable/dist/handsontable.full.min.js');
});
ipcMain.handle('inject-css', () => {
    return path.join(app.getAppPath(), '/node_modules/handsontable/dist/handsontable.full.min.css');
});
ipcMain.handle('inject-LanguageFile', () => {
    return path.join(app.getAppPath(), '/node_modules/handsontable/languages/zh-CN.js');
});
//打开名单页面
ipcMain.on('open-list-page', () => {
    require('../listPage/listPage').createListWindow(mainWindow);
})
//获取团期数据
ipcMain.handle('get-tour-dates', () => {
    return require('../temp/various').tourDates;
});
//当前选中的团期数据的ID
ipcMain.on('change-current-tour-data-id', (event,id) => {
    require('../temp/various').currentTourDateID = id;
})
//修改团期计划占位数
ipcMain.on('change-tour-dates-actualNum', (event, num) => {
    let various = require('../temp/various');
    various.tourDates[various.currentTourDateID - 1].actualNum = num;
    mainWindow.webContents.executeJavaScript('window.updateTourDates();')
})
//打开包含全部上车点单位的地图页面
ipcMain.on('open-total-map-page', () => {
    require('../mapPage/mapPage').createTotalMapWindow(mainWindow);
})
ipcMain.handle('get-Poi-Map', () => {
    let various = require('../temp/various');
    return various.tourDates[various.currentTourDateID - 1].mapPoi;
});

module.exports = {
    createMainWindow,
};